package com.laiketui.common.mapper;

import com.laiketui.domain.product.AuctionProductModel;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.db.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 竞拍商品
 *
 * @author Trick
 * @date 2021/2/23 9:50
 */
public interface AuctionProductModelMapper extends BaseMapper<AuctionProductModel> {

    @Override
    List<Map<String, Object>> selectDynamic(Map<String, Object> map) throws LaiKeAPIException;

    @Override
    int countDynamic(Map<String, Object> map) throws LaiKeAPIException;

    /**
     * 获取当前用户竞拍商品
     *
     * @param storeId   -
     * @param userId    -
     * @param endTime   -
     * @param pageStart -
     * @param pageEnd   -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/2/23 9:52
     */
    @Select("SELECT a.id, a.title, a.starttime, a.imgurl, a.endtime , a.price, a.add_price, a.user_id, a.status, a.invalid_time , a.pepole, a.is_buy, b.is_pay FROM lkt_auction_product a " +
            "LEFT JOIN lkt_auction_promise b ON a.id = b.a_id AND b.store_id = #{storeId} AND b.user_id = #{userId} AND b.is_pay = 1 " +
            "WHERE a.store_id = #{storeId} AND a.status = 1 AND a.recycle = 0 AND a.is_show = 1 AND a.invalid_time > #{endTime} " +
            "GROUP BY a.id ORDER BY a.id DESC, b.add_time DESC LIMIT #{pageStart}, #{pageEnd}")
    List<Map<String, Object>> selectUserAuctionGoodsInfo(int storeId, String userId, Date endTime, int pageStart, int pageEnd) throws LaiKeAPIException;


    /**
     * 获取商城竞拍商品
     *
     * @param storeId   -
     * @param endTime   -
     * @param pageStart -
     * @param pageEnd   -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/2/23 9:52
     */
    @Select("select a.id,a.starttime,a.imgurl,a.endtime,a.price,a.add_price,a.status,a.invalid_time,a.pepole,a.is_buy from lkt_auction_product as a " +
            "left join lkt_mch as b on a.mch_id = b.id where b.store_id = #{storeId}  and a.status = 1 and a.invalid_time > #{endTime} " +
            "and a.is_show = 1 and a.recycle = 0 and b.is_open = 1 group by a.id order by a.id desc LIMIT #{pageStart}, #{pageEnd}")
    List<Map<String, Object>> selectAuctionGoodsInfo(int storeId, Date endTime, int pageStart, int pageEnd) throws LaiKeAPIException;


    /**
     * 获取准备竞拍的商品信息
     *
     * @param storeId   -
     * @param pageStart -
     * @param pageEnd   -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/4/22 14:29
     */
    @Select("select a.id,a.title,a.imgurl,DATE_FORMAT(a.starttime,'%Y-%m-%d %H:%i:%S') starttime,a.price,a.add_price,a.pepole,a.status " +
            " from lkt_auction_product as a left join lkt_mch as b on a.mch_id = b.id " +
            "    where b.store_id = #{storeId} and a.status = 0 and a.recycle = 0 and a.is_show = 1 and b.is_open = 1 LIMIT #{pageStart}, #{pageEnd}")
    List<Map<String, Object>> selectAuctionReadyGoodsInfo(int storeId, int pageStart, int pageEnd) throws LaiKeAPIException;

    /**
     * 获取竞拍商品列表
     *
     * @param map -
     * @return List
     * @author Trick
     * @date 2021/4/22 10:57
     */
    List<Map<String, Object>> selectAuctionGoodsList(Map<String, Object> map);
    int countAuctionGoodsList(Map<String, Object> map);


    /**
     * 增减竞拍数量
     *
     * @param storeId -
     * @param id      -
     * @param num     -
     * @return int
     * @author Trick
     * @date 2021/4/23 15:04
     */
    @Update("update lkt_auction_product set pepole = pepole+#{num} where store_id = #{storeId} and id = #{id}")
    int addAuctionNumById(int storeId, int id, int num);

    /**
     * 更新当前商品竞拍价
     *
     * @param currentPrice -
     * @param storeId      -
     * @param id           -
     * @return int
     * @author Trick
     * @date 2021/4/23 16:17
     */
    @Update("update lkt_auction_product set current_price = #{currentPrice} where store_id = #{storeId} and id = #{id} and status = 1")
    int updateAuctionPrice(BigDecimal currentPrice, int storeId, int id);


    /**
     * 开始竞拍活动
     *
     * @param stroeId   -
     * @param id        -
     * @param startTime -
     * @return int
     * @author Trick
     * @date 2021/4/24 9:09
     */
    @Update("update lkt_auction_product set status = 1,starttime = #{startTime} where id = #{id} and store_id = #{stroeId} and recycle = 0")
    int startAuction(int stroeId, int id, Date startTime);


    /**
     * 更新竞拍得主,竞拍人数必须大于最低参与人数
     *
     * @param storeId -
     * @param id      -
     * @param price   -
     * @param userId  -
     * @return int
     * @author Trick
     * @date 2021/4/24 10:37
     */
    @Update("update lkt_auction_product set status = 2,current_price = #{price},user_id = '$user_id' where store_id = #{storeId} and id = #{id} and pepole >= low_pepole")
    int updateAuctionUser(int storeId, int id, BigDecimal price, String userId);


    /**
     * 流拍
     *
     * @param storeId -
     * @param id      -
     * @return int
     * @author Trick
     * @date 2021/4/24 14:57
     */
    @Update("update lkt_auction_product set status = 3 where store_id = #{storeId} and id = #{id} and pepole < low_pepole")
    int updateAuctionStreaming(int storeId, int id);

    /**
     * 修改竞拍订单
     *
     * @param orderno -
     * @param isBuy   -
     * @param storeId -
     * @param aid     -
     * @param userId  -
     * @return int
     * @author Trick
     * @date 2021/4/24 12:19
     */
    @Update("update lkt_auction_product set trade_no = #{orderno},is_buy = #{isBuy},user_id = #{userId} where store_id = #{storeId} and id = #{aid}")
    int updateAuctionOrder(String orderno, int isBuy, int storeId, int aid, String userId);

    //根据竞拍id获取店铺信息
    @Select("select b.id mchId,b.account_money money from lkt_auction_special a inner join lkt_mch b on a.mch_id=b.id where a.id=#{specialId}")
    Map<String, Object> getMchInfoBySpecialId(String specialId);

}